<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/2 13:54
 * Modified Time: 2019/7/2 13:54
 * Version: 2.0
 */

namespace App\Modules\Inventory\Services;


use App\Exceptions\AllValidatorException;
use App\Exceptions\DbException;
use App\Lib\Utilities;
use App\Modules\Inventory\Criterias\GetGoodsCriteria;
use App\Modules\Inventory\Criterias\GetGoodsListCriteria;
use App\Modules\Inventory\Criterias\GetSellGoodsListCriteria;
use App\Modules\Inventory\Repositories\GoodsCategoryRepository;
use App\Modules\Inventory\Repositories\GoodsRepository;
use App\Modules\Inventory\Repositories\GoodsStockRepository;
use Flower\Core\Repository\Eloquent\BaseService;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;
use mysql_xdevapi\Exception;

class GoodsService extends BaseService
{

    protected $goodsRepository;
    protected $goodsCategoryRepository;
    protected $goodsCategoryService;
    protected $goodsStockRepository;

    public function __construct(GoodsRepository $goodsRepository,
                                GoodsCategoryRepository $goodsCategoryRepository,
                                GoodsStockRepository $goodsStockRepository,
                                GoodsCategoryService $goodsCategoryService)
    {
        $this->goodsRepository = $goodsRepository;
        $this->goodsCategoryRepository = $goodsCategoryRepository;
        $this->goodsCategoryService = $goodsCategoryService;
        $this->goodsStockRepository = $goodsStockRepository;
    }

    /*
     * 创建商品
     */
    public function createGoods(
        $store_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty,
        $volume, $specs, $is_sell, $image
    )
    {
        DB::beginTransaction();

        try {
            if ($is_sell) {
                if ($market_price <= $royalty) {
                    throw new AllValidatorException(['msg' => '提成不能小于销售额']);
                }
            }

            $goods = $this->goodsRepository->createGoods(
                $store_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty,
                $volume, $specs, $is_sell, $image
            );

            $goods_id = $goods->id;

            //增加库存表记录
            $this->goodsStockRepository->createGoodsStock($store_id,$goods_id);

            DB::commit();
            return $goods;
        } catch (Exception $e) {
            DB::rollBack();
            throw new DbException();
        }

    }

    /*
     * 编辑商品
     */
    public function editGoods($goods_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty, $volume, $specs, $is_sell, $image)
    {

        return $this->goodsRepository->editGoods($goods_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty, $volume, $specs, $is_sell, $image);

    }

    /*
     * 删除商品
     */
    public function delGoods($goods_id)
    {
        return $this->goodsRepository->delete($goods_id);
    }

    /*
     * 获取商品
     */
    public function getGoods($goods_id)
    {

        $goods = $this->goodsRepository->pushCriteria(new GetGoodsCriteria($goods_id))->first();

        return $goods->toArray();
    }

    /*
     * 获取本店所有商品搜索
     */
    public function getGoodsListSearch($store_id, $keywords, $f_class, $s_class)
    {

        $list = $this
            ->goodsCategoryRepository
            ->pushCriteria(
                new GetGoodsListCriteria($store_id, $keywords, $f_class, $s_class)
            )->get();

        return $list;
    }

    public function getSellGoodsList($store_id, $keywords, $pageSize)
    {
        $data = $this->goodsRepository->pushCriteria(new GetSellGoodsListCriteria($store_id, $keywords))->paginate($pageSize, ['*']);

        return Utilities::dealPage($data);
    }

    /*
     * 判断商品是否存在
     */
    public function checkGoodsExist($store_id, $goods_id)
    {
        $map = [
            ['id', '=', $goods_id],
            ['store_id', '=', $store_id]
        ];
        $goods = $this->goodsRepository->findByField($map)->first();

        if (!$goods) {
            throw new AllValidatorException(['msg' => '商品不存在']);
        }

        return $goods;

    }

    /*
     * 判断一级父分类是否存在
     */
    protected function checkParentId($store_id, $parent_id)
    {
        if ($parent_id != 0) {
            $map = [
                ['id', '=', $parent_id],
                ['store_id', '=', $store_id],
                ['parent_id', '=', 0]
            ];
            $category = $this->goodsCategoryRepository->findByField($map)->first();
            if (!$category) {
                throw new AllValidatorException(['msg'=>'父级分类不存在']);
            }
        }

    }

    /*
     * 判断分类是否可删
     */
    protected function deletable($categotry_id)
    {
        //获取分类实例
        $categotry = $this->goodsCategoryRepository->findByField('id', $categotry_id)->first();


        if ($categotry->parent_id == 0) {//一级分类
            //判断下面有无子分类
            $subset = $this->goodsCategoryRepository->findByField('parent_id', $categotry_id);
            if ($subset->count() > 0) {
                throw new AllValidatorException(['msg'=>'不能删除有子分类的分类']);
            }
        } else { //二级分类
            //判断有无商品
            $goods = $this->goodsRepository->findByField('cat_id', $categotry_id);
            if ($goods->count() > 0) {
                throw new AllValidatorException(['msg'=>'不能删除含有商品的分类']);
            }
        }
    }

}